/*
 * $Author: isan.wong@gmail.com $
 * $Date: 2011-07-29 01:03:06 +0000 (Fri, 29 Jul 2011) $
 * $Rev: 13 $
 * $URL: http://capitalbio-smd.googlecode.com/svn/src/com/capitalbio/smd/gui/SamplePane.java $
 */

package com.capitalbio.smd.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.capitalbio.hla.chromatogramViewer.VisualPane;
import com.capitalbio.smd.base.Sample;

/**
 * 样品面板
 * 
 * @author Wang Lei
 */
public class SamplePane extends JPanel {
	private static final long serialVersionUID = 1L;
	protected Log log = LogFactory.getLog(this.getClass());

	private MainFrame mainFrame;
	private Sample sample;
	private VisualPane visualPane;
	private AlignmentPane alignmentPane;
	private LogPane logPane;
	private ActionListener alignmentButtonActionListener;

	/**
	 * 新建样品面板 初始化后, 没有样品被选择时, 默认显示博奥标志
	 */
	public SamplePane(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
		setBorder(new EmptyBorder(0, 0, 0, 0));
		setLayout(new BorderLayout());
		
		visualPane = new VisualPane();
		alignmentPane = new AlignmentPane();
		logPane = new LogPane();

		add(visualPane, BorderLayout.NORTH);

		JPanel centerPane = new JPanel();
		centerPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		centerPane.setLayout(new BorderLayout());

		JScrollPane logScrollPane = new JScrollPane(logPane);
		logScrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));

		centerPane.add(alignmentPane, BorderLayout.NORTH);
		centerPane.add(logScrollPane, BorderLayout.CENTER);

		add(centerPane, BorderLayout.CENTER);

		alignmentButtonActionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				AlignmentButton activeButton = (AlignmentButton) evt
						.getSource();

				log.debug("Current button: " + activeButton.getName());

				int validIndex = sample.getResult()
					.getValidIndex(activeButton.getIndex());
				visualPane.getChromatPane().highlightBasesRegions(new int[] {validIndex + 1, validIndex + 1});
			}
		};
	}

	/**
	 * 获取样品
	 * 
	 * @return 样品
	 */
	public Sample getSample() {
		return sample;
	}

	/**
	 * 设置样品
	 * 
	 * @param sample
	 *            样品
	 */
	/*
	 * private BoundedRangeModel alignmentPaneScrollModel; private
	 * BoundedRangeModel chromatogramScrollModel; private float scale;
	 */
	public void setSample(Sample sample){
		this.sample = sample;
		visualPane.setChromat(sample.getChromat());
		alignmentPane.setSample(sample);
		logPane.setSample(sample);

		List<AlignmentButton> alignmentButtons = alignmentPane
				.getAlignmentButtons();
		if (alignmentButtons != null) {
			for (AlignmentButton alignmentButton : alignmentButtons) {
				alignmentButton
						.addActionListener(alignmentButtonActionListener);
			}
		}

		// 同步 ScrollBar
		/*
		 * alignmentPaneScrollModel =
		 * alignmentPane.getHorizontalScrollBar().getModel();
		 * chromatogramScrollModel =
		 * chromatogramPane.getHorizontalScrollBar().getModel();
		 * 
		 * //log.debug(alignmentPaneScrollModel.getMaximum() + " " +
		 * chromatogramScrollModel.getMaximum()); scale = 1.0F;
		 * 
		 * ChangeListener baseCallScrollModelChangeListener = new
		 * ChangeListener() {
		 * 
		 * @Override public void stateChanged(ChangeEvent evt) {
		 * BoundedRangeModel model = (BoundedRangeModel) evt.getSource(); int
		 * value = (int) Math.ceil((double) (model.getValue() / scale));
		 * chromatogramScrollModel.setValue(value);
		 * //log.debug(model.getValue()); } };
		 * alignmentPaneScrollModel.addChangeListener
		 * (baseCallScrollModelChangeListener);
		 * 
		 * ChangeListener chromatogramScrollModelChangeListener = new
		 * ChangeListener() {
		 * 
		 * @Override public void stateChanged(ChangeEvent evt) {
		 * BoundedRangeModel model = (BoundedRangeModel) evt.getSource(); int
		 * value = (int) Math.ceil((double) (model.getValue() / scale));
		 * alignmentPaneScrollModel.setValue(value);
		 * //log.debug(model.getValue()); } };
		 * chromatogramScrollModel.addChangeListener
		 * (chromatogramScrollModelChangeListener);
		 */
	}

	public VisualPane getVisualPane() {
		return visualPane;
	}

	public AlignmentPane getAlignmentPane() {
		return alignmentPane;
	}

	public LogPane getLogPane() {
		return logPane;
	}
}
